package tp.pr5.items;

import tp.pr5.City;
import tp.pr5.NavigationModule;
import tp.pr5.RobotEngine;
import tp.pr5.Street;

public class CodeCard extends Item {
	private String code;
	/**
	 * Code card constructor
	 * @param id
	 * @param description
	 * @param code
	 */
	public CodeCard(String id, String description, String code) {//constructor
		super(id, description);
		this.code = code;
	}
	/**
	 * A code card always can be used. Since the robot has the code card it never loses it
	 */
	@Override
	public boolean canBeUsed() {		
		return true;
	}	
	/**
	 * To String override
	 */
	public String toString(){
		return this.getId()+": "+super.toString();
		
	}
	public String getCode(){
		return this.code;
	}
	/**
	 * Opens the street if it's closed, it closes it if it's open
	 */
	@Override
	public boolean use(RobotEngine r, NavigationModule nav) {
		City city = nav.getaCity(); 
		Street calle = null;
		if(city != null){
			calle =city.lookForStreet(nav.getCurrentPlace(), nav.getCurrentHeading());
		}
		if(calle != null){
			if(calle.isOpen()) return calle.close(this);
			else               return calle.open(this);
		}	
		return false;
	}
	@Override
	public boolean unUse(RobotEngine r, NavigationModule nav) {
		return use(r, nav);
	}

}
